/*!
 * 由字符串计算儒略日和修正儒略日
 * 时间格式:
 * - 字符串
 * - CCYY-MM-DDThh:mm:sss<sss>
 */

#include <stdio.h>

bool str2mjd(const char* utc, double& mjd) {
	int iY, iM, iD, ih, im, my, iypmy;
	double ss, fd;
	int n = sscanf(utc, "%d-%d-%dT%d:%d:%lf", &iY, &iM, &iD, &ih, &im, &ss);
	if (n < 6) return false;
	fd = ((ih * 60 + im) * 60 + ss) / 86400.;
	my = (iM - 14) / 12;
	iypmy = iY + my;

	mjd = (double)((1461 * (iypmy + 4800)) / 4
			+ (367 * (long) (iM - 2 - 12 * my)) / 12
			- (3 * ((iypmy + 4900) / 100)) / 4
			+ iD - 2432076) + fd;

	return true;
}

bool str2jd(const char* utc, double& jd) {
	double mjd;
	if (!str2mjd(utc, mjd)) return false;
	jd = mjd + 2400000.5;
	return true;
}

int main(int argc, char **argv) {
	if (argc < 2) {
		printf ("Usage:\n"
				"  %s time-utc\n\n"
				"  time-utc style: CCYY-MM-DDThh:mm:sss<sss>\n",
				argv[0]);
		return -1;
	}
	double mjd, jd;

	if (str2mjd(argv[1], mjd)) {
		printf ("MJD = %.9f\n", mjd);
	}
	if (str2jd(argv[1], jd)) {
		printf ("JD = %.9f\n", jd);
	}

	return 0;
}
